package com.morontestandroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FourthLevel extends Activity {
	private static final String DEBUG_TAG = "Level 4";
	
	Chronometer mChronometer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fourth_level);

		FrameLayout frame = (FrameLayout) findViewById(R.id.graphics_holder);
		mChronometer = (Chronometer) findViewById(R.id.level4ChrmId);
		PlayAreaView image = new PlayAreaView(this);
		
		mChronometer.setBase(getTime()); 
		mChronometer.start();
		
		frame.addView(image);
		frame.bringToFront();
		
	       TextView txt = (TextView) findViewById(R.id.question);  
	       Typeface font = Typeface.createFromAsset(getAssets(), "testtext.ttf");  
	        txt.setTypeface(font);
	}
	
	public long getTime() {
		  Bundle extras = getIntent().getExtras();
		  long time = extras.getLong("time");
		  return time;
		 
	}
	
	public void startFifthLevel() {
		int activityID = 1;
		Intent intent;
		intent = new Intent().setClass(this, FifthLevel.class);
		intent.putExtra("time", getThisBase());
		startActivityForResult(intent, activityID);
	}

	private class PlayAreaView extends View {

		private GestureDetector gestures;
		private Matrix translate;
		public Bitmap droid;

		private Matrix animateStart;
		private Interpolator animateInterpolator;
		private long startTime;
		private long endTime;
		private float totalAnimDx;
		private float totalAnimDy;

		public void onAnimateMove(float dx, float dy, long duration) {
			animateStart = new Matrix(translate);
			animateInterpolator = new OvershootInterpolator();
			startTime = System.currentTimeMillis();
			endTime = startTime + duration;
			totalAnimDx = dx;
			totalAnimDy = dy;
			post(new Runnable() {
				@Override
				public void run() {
					onAnimateStep();
				}
			});
		}

		private void onAnimateStep() {
			long curTime = System.currentTimeMillis();
			float percentTime = (float) (curTime - startTime)
					/ (float) (endTime - startTime);
			float percentDistance = animateInterpolator
					.getInterpolation(percentTime);
			float curDx = percentDistance * totalAnimDx;
			float curDy = percentDistance * totalAnimDy;
			translate.set(animateStart);
			onMove(curDx, curDy);

			// Log.v(DEBUG_TAG, "We're " + percentDistance +
			// " of the way there!");
			if (percentTime < 1.0f) {
				post(new Runnable() {
					@Override
					public void run() {
						onAnimateStep();
					}
				});
			}
		}

		public void onMove(float dx, float dy) {
			translate.postTranslate(dx, dy);
			invalidate();
		}

		public void onResetLocation() {
			translate.reset();
			invalidate();
		}

		public void onSetLocation(float dx, float dy) {
			translate.postTranslate(dx, dy);
		}

		public PlayAreaView(Context context) {
			super(context);
			translate = new Matrix();
			gestures = new GestureDetector(FourthLevel.this,
					new GestureListener(this));
			droid = BitmapFactory.decodeResource(getResources(),
					R.drawable.monster0);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawBitmap(droid, translate, null);

			Matrix m = canvas.getMatrix();
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			return gestures.onTouchEvent(event);
		}
		

	}

	private class GestureListener implements GestureDetector.OnGestureListener,
			GestureDetector.OnDoubleTapListener {

		private static final int SWIPE_MIN_DISTANCE = 120;
		private static final int SWIPE_MAX_OFF_PATH = 250;
		private static final int SWIPE_THRESHOLD_VELOCITY = 200;
		private boolean swipe1 = true;
		private boolean swipe2 = false;
		private boolean swipe3 = false;
		private boolean swipe4 = false;
		private boolean swipe5 = false;
		private boolean swipe6 = false;
		PlayAreaView view;

		public GestureListener(PlayAreaView view) {
			this.view = view;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			Log.v(DEBUG_TAG, "onDown");
			return true;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2,
				final float velocityX, final float velocityY) {
			if (!swipe6) {
				if (swipe1) {
					try {
						if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
							return false;
						if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
								&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
							swipe1 = true;
							view.droid = BitmapFactory.decodeResource(
									getResources(), R.drawable.monster0);
							view.invalidate();
						} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
								&& Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
							swipe1 = false;
							swipe2=true;
							view.droid = BitmapFactory.decodeResource(
									getResources(), R.drawable.monster1);
							view.invalidate();
						}
					} catch (Exception e) {
						// nothing
					}
				}
				
				else if(swipe2){
        	 try {
                 if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
                     return false;
                 if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe1=true;
                	 swipe2=false;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster0);
                     view.invalidate();
                 }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe2=false;
                	 swipe3=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster2);
                     view.invalidate();
                 }
             } catch (Exception e) {
                 // nothing
             }
        		}
        		
				else if(swipe3){
        	 try {
                 if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
                     return false;
                 if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe2=true;
                	 swipe3=false;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster1);
                     view.invalidate();
                 }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe3=false;
                	 swipe4=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster3);
                     view.invalidate();
                 }
             } catch (Exception e) {
                 // nothing
             }
        		}
        		
				else if(swipe4){
        	 try {
                 if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
                     return false;
                 if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe4=false;
                	 swipe3=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster2);
                     view.invalidate();
                 }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe4=false;
                	 swipe5=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster4);
                     view.invalidate();
                 }
             } catch (Exception e) {
                 // nothing
             }
        		}
        		
				else if(swipe5){
        	 try {
                 if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
                     return false;
                 if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe5=false;
                	 swipe4=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster3);
                     view.invalidate();
                 }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                	 swipe5=false;
                	 swipe6=true;
                     view.droid = BitmapFactory.decodeResource(getResources(),R.drawable.monster5);
                     view.invalidate();
                     
                     startFifthLevel();
                 }
             } catch (Exception e) {
                 // nothing
             }
        		}

			}
			return false;
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			Log.v(DEBUG_TAG, "onDoubleTap");
			view.onResetLocation();
			return true;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			Log.v(DEBUG_TAG, "onLongPress");
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// Log.v(DEBUG_TAG, "onScroll");

			// view.onMove(-distanceX, -distanceY);
			return true;
		}

		@Override
		public void onShowPress(MotionEvent e) {
			Log.v(DEBUG_TAG, "onShowPress");
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			Log.v(DEBUG_TAG, "onSingleTapUp");
			return false;
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			Log.v(DEBUG_TAG, "onDoubleTapEvent");
			return false;
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			Log.v(DEBUG_TAG, "onSingleTapConfirmed");
			return false;
		}

	}
	
	public long getThisBase(){
		  return mChronometer.getBase();
		 
	}

}